﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnCarMgr : MonoBehaviour
{
    private int step1time = 12;
    private int step2time = 24;
    private int step3time = 36;
    private int step4time = 80;

    //private int step1time = 1;
    //private int step2time = 2;
    //private int step3time = 3;
    //private int step4time = 4;


    public List<GameObject> LstPrefab;
    public GameObject PrefabCar;
    public GameObject PrefabCarTruck;

    public SpawnPosition[] ArraySpawnPos;
	
	public SpawnPosition NorthPos;
	public SpawnPosition SouthPos;
	public SpawnPosition EastPos;
	public SpawnPosition WestPos;

    public float SpawnCarTime;

    public ChangeSpeedMgr chgSpeedMgr;

    public bool DebugTest;
	public GlobalDefine.CarMovementDir MoveDir;
    public CrashEvent CrashEvt;
    public PlayerController PlayerCtrl;
    private float m_fCurSpawnCarTime;
    private float m_fgameTime;
    private int m_nCntLst;
    private bool m_bActive;

    private int m_SouthCount = 0;
    private int m_NorthCount = 0;
    private int m_WestCount = 0;

    private int m_Presscount = 0;
    // Start is called before the first frame update
    void Start()
    {
        m_nCntLst = ArraySpawnPos.Length;
        m_bActive = true;


    }

    public bool IsActive()
    {
        return m_bActive;
    }

    // Update is called once per frame
    void Update()
    {
        if(m_bActive == true)
        {
            if (DebugTest == true)
            {
                DebugTest = false;
                //_randomSpawn();

                GenerateByDir(MoveDir, GetRandomPrefabCar());
            }

            if (Input.GetKeyDown(KeyCode.Space) == true)
            {
                m_Presscount++;
            }
           
        }
        else
        {
            m_Presscount = 0;
        }
    }
    private GameObject GetRandomPrefabCar()
    {
        int nIdx = Random.Range(0, LstPrefab.Count);
        Debug.Log(string.Format("nIdx{0}",nIdx));
        return LstPrefab[nIdx];
    }
	public void GenerateByDir(GlobalDefine.CarMovementDir _dir,GameObject _obj)
	{
		if( _dir == GlobalDefine.CarMovementDir.CarMovementDir_North )
		{
			_generate(NorthPos, GetRandomPrefabCar());
		}
		else if( _dir == GlobalDefine.CarMovementDir.CarMovementDir_South )
		{
			_generate(SouthPos, GetRandomPrefabCar());
		}
		else if( _dir == GlobalDefine.CarMovementDir.CarMovementDir_East )
		{
			_generate(EastPos, GetRandomPrefabCar());
		}
		else if( _dir == GlobalDefine.CarMovementDir.CarMovementDir_West )
		{
			_generate(WestPos, GetRandomPrefabCar());
		}
	}
		
	private void _randomSpawn()
	{
        int nRandom = Random.Range(0, m_nCntLst);
        SpawnPosition _pos = ArraySpawnPos[nRandom];
		_generate(_pos, GetRandomPrefabCar());
	}
    private void _generate(SpawnPosition _pos,GameObject _objPrefab)
    {
        GameObject _objCar = Instantiate(_objPrefab, Vector3.zero, Quaternion.identity);
        _objCar.transform.SetParent(_pos.transform);
        _objCar.transform.localPosition = Vector3.zero;
        CarMovement _carMovement = _objCar.GetComponent<CarMovement>();
        _carMovement.Registe(chgSpeedMgr);
        _carMovement.RegisteEvent(CrashEvt);
        _carMovement.SetStartData(_pos.MoveDir, _pos.Dir);
        CrashEvt.Registe(_gameover);
        PlayerCtrl.RegisteCrashEvent(CrashEvt);
    }
    public void PublicGenerate(SpawnPosition _pos, GameObject _objPrefab)
    {
        _generate(_pos, _objPrefab);
    }
    private void _gameover()
    {
        m_bActive = false;
    }
    private void OnDestroy()
    {
        CrashEvt.Unregiste(_gameover);
    }
    private void FixedUpdate()
    {
        //if(m_bActive == true)
        //{
        //    m_fCurSpawnCarTime += Time.fixedDeltaTime;
        //    if (m_fCurSpawnCarTime >= SpawnCarTime)
        //    {
        //        m_fCurSpawnCarTime -= SpawnCarTime;
        //        _randomSpawn();
        //    }
        //}

        if(m_bActive == true)
        {
            m_fgameTime += Time.fixedDeltaTime;
            m_fCurSpawnCarTime += Time.fixedDeltaTime;
            NewGenerateCar();
        }
    }
    private void NewGenerateCar()
    {
        if (m_fCurSpawnCarTime >= SpawnCarTime)
        {
            m_fCurSpawnCarTime -= SpawnCarTime;
        }
        else
        {
            return;
        }

            //第一难度新手引导 大约12秒  单方向
        if (m_fgameTime < step1time && m_Presscount < 6 )
        {
            int nRandom = Random.Range(1, 3);
            if (nRandom == 1)
            {
                m_NorthCount++;
                _generate(NorthPos, GetRandomPrefabCar());
            }
            else if (m_NorthCount< 0) //避免不刷车 等着无聊
            {
                m_NorthCount++;
                _generate(NorthPos, GetRandomPrefabCar());      
            }
            else
                m_NorthCount--;

            if (m_WestCount == 0)
            {
                m_WestCount++;
                _generate(WestPos, PrefabCarTruck);
            }
            else
            {
                m_WestCount--;
            }
            //else if( nRandom2 == 2)
            //{
            //    _generate(EastPos, PrefabCarTruck);
            //}
            
        }
        else if ( m_fgameTime < step2time && m_Presscount < 15) //第二阶段 南北两面刷车 转身教学
        {

            int nRandom = Random.Range(1, 5);
            if (nRandom == 1)
            {
                _generate(NorthPos, GetRandomPrefabCar());
            }
            else if (nRandom == 2)
            {
                _generate(SouthPos, GetRandomPrefabCar());
            }

            if (m_WestCount == 0)
            {
                m_WestCount++;
                _generate(WestPos, PrefabCarTruck);
            }
            else
            {
                m_WestCount--;
            }
        }
        else if ( m_fgameTime < step3time && m_Presscount < 25) //第三阶段 东西两面刷车 高压力
        {
            int nRandom = Random.Range(1, 6);
            if (nRandom < 4)
            {
                m_NorthCount++;
                _generate(NorthPos, GetRandomPrefabCar());
            }
            else if (m_NorthCount > 0)
            {
                m_NorthCount--;
            }

            int nRandomSouth = Random.Range(1, 6);
            if (nRandomSouth == 1)
            {
                m_SouthCount++;
                _generate(SouthPos, GetRandomPrefabCar());
            }
            else if (m_SouthCount > 0)
            {
                m_SouthCount--;
            }

            if (m_SouthCount > 2 || m_NorthCount > 2)
            {
                m_NorthCount = 0;
                m_SouthCount = 0;
            }
            else
            {
                int nRandomWest = Random.Range(0, 5);
                if (nRandomWest < 2)
                {
                    _generate(WestPos, PrefabCarTruck);
                }
            }
        }
        else // 最终难度
        {
            //高几率刷北面车
            int nRandom = Random.Range(1, 6);
            if (nRandom < 5)
            {
                m_NorthCount++;
                _generate(NorthPos, GetRandomPrefabCar());
            }
            else if (m_NorthCount > 0)
            {
                m_NorthCount--;
            }

            int nRandomSouth = Random.Range(1, 6);
            if (nRandomSouth == 1 )
            {
                m_SouthCount++;
                _generate(SouthPos, GetRandomPrefabCar());
            }
            else if (m_SouthCount > 0)
            {
                m_SouthCount--;
            }

            if (m_SouthCount > 2 || m_NorthCount > 2)
            {
                m_NorthCount = 0;
                m_SouthCount = 0;
            }
            else
            {
                int nRandomWest = Random.Range(0, 5);
                if (nRandomWest < 4)
                {
                    _generate(WestPos, PrefabCarTruck);
                }
                else
                {
                    if (m_fgameTime > step4time)  //最终难度二阶段 左右开车
                    {
                        int nRandomEasth = Random.Range(0, 5);
                        if (nRandomEasth < 5)
                        {
                            _generate(EastPos, PrefabCarTruck);
                        }
                    }
                       
                }
            }
        }
    }
}
